<template>
  <div>
    <el-dialog
      title='添加分组'
      :close-on-click-modal="false"
       width="540px"
      :visible.sync="this.dialogVisible"
      append-to-body
      @open="getInit"
      :before-close="handleClose"
      >
      <el-form style="margin:0px 20px 50px;" :model="ruleForm" :rules="rules" ref="ruleForm" >
        <el-form-item label="新分组名称:"  prop="name">
          <el-input
            placeholder="请输入分组名称"
            v-model="ruleForm.name"
            clearable>
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose()">取 消</el-button>
        <el-button type="primary" @click="submitAddGroup('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getAction, postAction, getUrlAction, postUrlAction } from '@/api/common'
export default {
  name: 'AddGroup',
  props: {
    show: {
      type: Boolean,
      default: false
    }
  },
  components: {
  },
  created() {
    this.getInit()
  },
  watch: {
    show() {
      this.dialogVisible = this.show
    }
  },
  data: function() {
    return {
      ruleForm: {
        name: undefined
      },
      groupId: null,
      tableData: [],
      options: [],
      dialogVisible: this.show,
      rules: {
        name: [
          { required: true, message: '请输入新分组名称', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleClick(row) {
      console.log(row)
    },
    getInit() {
      // getAction({}, 'EmailConfigGroup/GetGroupList')
      //   .then((res) => {
      //     this.options = res.data
      //   })
    },
    handleClose() {
      this.resetForm('ruleForm')
      this.$emit('update:show', false)
    },
    submitAddGroup(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          postAction(this.ruleForm, 'EmailConfigGroup/Create')
            .then((res) => {
              if (res.code === 20000) {
                this.$message({
                  message: '新增分组成功!',
                  type: 'success'
                })
                this.ruleForm = {
                  name: undefined
                }
                this.handleClose(false)
                this.$emit('closeAction')
              } else {
                this.$message({
                  message: res.msg,
                  type: 'warning'
                })
              }
            })
        } else {
          return false
        }
      })
      // if (this.name == null) {
      //   this.$message({
      //     type: 'info',
      //     message: '请选择分组后提交'
      //   })
      //   return false
      // }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped>

</style>
